package com.kuang.demo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.demo.bo.gh.CreateIssueBO;
import com.kuang.demo.bo.gh.UpdateIssueBO;
import com.kuang.demo.mapper.IssueMapper;
import com.kuang.demo.vo.CreateIssueVO;
import com.kuang.demo.vo.QueryIssueVO;
import com.kuang.demo.pojo.IssuePOJO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 郭浩 on 2020/11/3 8:57
 */
@Service
public class IssueServiceImpl implements IssueService{
    @Autowired
    IssueMapper issueMapper;
    @Override
    public Map<String, Object> queryIssueList(Integer page, Integer limit, String question, String sort, String order) {
        PageHelper.startPage(page,limit);
        List<QueryIssueVO.ItemsBean> itemsBeans = issueMapper.queryIssueList(question,sort,order);

        PageInfo<QueryIssueVO.ItemsBean> itemsBeanPageInfo = new PageInfo<>(itemsBeans);
        Long i = itemsBeanPageInfo.getTotal();
        int total = i.intValue();
        Map<String,Object> issueInfo = new HashMap();
        issueInfo.put("issueList",itemsBeans);
        issueInfo.put("total",total);
        return issueInfo;
    }

    @Override
    public IssuePOJO updateIssue(UpdateIssueBO updateIssueBO) {
        issueMapper.updateIssueById(updateIssueBO);
        IssuePOJO issuePOJO = issueMapper.queryIssueById(updateIssueBO.getId());
        return issuePOJO;
    }

    @Override
    public void deleteIssue(IssuePOJO issuePOJO) {
        issueMapper.deleteIssue(issuePOJO.getId());
    }

    @Override
    public CreateIssueVO createIssue(CreateIssueBO createIssueBO) {
        issueMapper.createIssue(createIssueBO);

        return issueMapper.queryIssueByIdWithoutDeleted(createIssueBO.getId());
    }
}
